package com.disease.chronic.Service.Impl;

import com.disease.chronic.Exception.ChronicExceptionEnum;
import com.disease.chronic.Global.Result;
import com.disease.chronic.Mappers.FamilyMapper;
import com.disease.chronic.Service.FamilyService;
import com.disease.chronic.pojo.Family;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FamilyServiceImpl implements FamilyService {
    @Autowired
    FamilyMapper familyMapper;
    @Override
    public Result getFamily(String username) {
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage()
                ,familyMapper.selectByUsername(username));
    }

    @Override
    public Result addFamily(Family family) {
        Family family1 = familyMapper.selectByPrimaryKey(family.getPhoneNumber());
        if(family1!=null){
            return new Result(ChronicExceptionEnum.PHONE_EXIST.getCode(),
                    ChronicExceptionEnum.PHONE_EXIST.getMessage());
        }
        familyMapper.insert(family);
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage());
    }

    @Override
    public Result deleteByPhone(String phone) {
        familyMapper.deleteByPrimaryKey(phone);

        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage());
    }
}
